import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import $ from 'jquery'
import { DatePicker} from 'element-ui';
import api from "./api.js"
import audio from "./audio.js"
import Schedule from "./schedule.js"
// import socket from "./socket.io.js"

window.$ = $;
document.oncontextmenu = function(){
    　　return false;
    }
document.addEventListener('DOMContentLoaded', function (event) {
    // chrome 浏览器直接加上下面这个样式就行了，但是ff不识别
    document.body.style.zoom = 'reset';
    document.addEventListener('keydown', function (event) {
        if ((event.ctrlKey === true || event.metaKey === true)
            && (event.which === 61 || event.which === 107
                || event.which === 173 || event.which === 109
                || event.which === 187 || event.which === 189)) {
            event.preventDefault();
        }
    }, false);
    document.addEventListener('mousewheel DOMMouseScroll', function (event) {
        if (event.ctrlKey === true || event.metaKey) {
            event.preventDefault();
        }
    }, false);
}, false);

// 获取公共配置
let UserId=sessionStorage.getItem("UserId");
let RandCode =sessionStorage.getItem("Tonken");
let wsNum=sessionStorage.getItem("ws");



Vue.config.productionTip = false;
Vue.use(DatePicker);
Vue.prototype.api = api; // 挂载api
Vue.prototype.ajax=$.ajax;
Vue.prototype.audio=audio;
/* eslint-disable no-new */
Vue.directive("logo-ray",{
  inserted(el){
    let canvas=document.createElement("canvas");
    let w=el.clientWidth*1.5;
    let h=el.clientHeight*1.5;
    canvas.width=w;
    canvas.height=h;
    canvas.style.width=el.clientWidth+"px";
    canvas.style.height=el.clientHeight+"px";
    el.appendChild(canvas);
    let getCanvas=canvas.getContext("2d");
    let i=new Image();
    i.src=require("@/assets/img/ray.png");
    i.onload=function(){
      let x=-90;
      getCanvas.drawImage(i,x,0,w,h);
      setInterval(()=>{
        x++;
        if(x>w){
          x=-90;
        }
        getCanvas.clearRect(0,0,w,h);
        getCanvas.drawImage(i,x,0,w,h);
      },8)
    }
  }
})
window.SignalSocket = /** @class */ (function () {
    function SignalSocket(url, uid, headr, headTime, messFun, errorFun, sendFun, onclose, Slow, reStar, reYes,Success) {
        var _this = this;
        this.url = url;
        this.uid = uid;
        this.headr = headr;
        this.headTime = headTime;
        this.messFun = messFun;
        this.errorFun = errorFun;
        this.sendFun = sendFun;
        this.onclose = onclose;
        this.Slow = Slow;
        this.reStar = reStar;
        this.reYes = reYes;
        this.socketStatuc = 0; 
				this.setTimeNull = 0; 
				this.bangbang = null; //取消心跳time
				this.Success=Success;
		
        $.connection.hub.url = url;
        this.socket = $.connection.chatHub;
        this.socket.client.broadcastMessage = function (message, data) {
            _this.messFun(message, data);
		};
        $.connection.hub.start({})
            .done(function () {
			_this.sendHrad();
			_this.Success();
        });
        $.connection.hub.received(function (Msg) {
            clearTimeout(_this.Monitor);
        });
        if (this.errorFun) {
            $.connection.hub.error(function (error) {
                _this.errorFun(error);
            });
        }
        if (this.onclose) {
            $.connection.hub.disconnected(function () {
               _this.onclose();
            });
        }
        if (this.Slow) {
            $.connection.hub.connectionSlow(function () {
                _this.Slow();
            });
        }
        if (this.reStar) {
            $.connection.hub.reconnecting(function () {
                _this.reStar();
            });
        }
        if (this.reYes) {
            $.connection.hub.reconnected(function () {
                _this.reYes();
            });
        }
    }
    // 发送心跳包程序
    SignalSocket.prototype.sendHrad = function () {
		var _this = this;
		if (this.socketStatuc == 0) { //链接成功启动
            setInterval( function(){
                _this.uSend(_this.headr, true);
			}, this.headTime);
        }
    };
    // 监控心跳包回复程序
    SignalSocket.prototype.getHeadMsg = function () {
        var _this = this;
        this.Monitor = setTimeout( () => {
			console.log(_this.setTimeNull)
            if (_this.setTimeNull >= 3) { // 超时次数大于3，判断为网络不可用
                $.connection.hub.stop();
            }
            _this.setTimeNull += 1;
        }, 5000);
    };
    SignalSocket.prototype.uSend = function (Msg, init) {
        if (init) {
            this.socket.server.send(this.uid, Msg); //发送心跳包
            this.getHeadMsg(); //启动监控心跳包回复定时器
        }
        else {
						this.socket.server.send(this.uid, Msg); //发送参数数据
            this.sendFun(Msg);
        }
	};
	SignalSocket.prototype.allStop=function(){
			$.connection.hub.stop();
	}
    return SignalSocket;
}());

window.tomSocket = (function(){
	function tomSocket(url,suceen,head,time,mess,error){
		var _this=this;
		this.head=head;
		this.time=time;
		this.mess=mess;
		this.suceen=suceen;
		this.error=error;
		this.set;
		this.setOut;
		this.socket=new WebSocket(url);
		this.socket.onopen=function(){
			if(_this.suceen){
				_this.suceen();
			}
			_this.headSet();
		}
		this.socket.onmessage=function(data){
			if(_this.mess){
				_this.mess(data)
			}
			clearTimeout(_this.set);
			clearTimeout(_this.setOut);
		}
		this.socket.onerror=function(){
			if(_this.error){
				_this.error()
			}
		}
		this.socket.onclose=function(msg){
			if(_this.error){
				_this.error(msg)
			}
		}
	}
	tomSocket.prototype.uSend=function(msg){
		if(msg==this.head){
			this.setOut=setTimeout(()=>{
				this.socket.close();
			},20000)
		}
		this.socket.send(msg);
	}
	tomSocket.prototype.close=function(){
		this.socket.close();
	}
	tomSocket.prototype.headSet=function(){
		this.set=setTimeout(()=>{
			this.uSend(this.head);
		},this.time)
	}
	tomSocket.prototype.getStatus=function(){
		return this.socket.readyState;
	}
	return tomSocket;
}())


window.GameAudio=(function(){
	var GameAudio=function(url,load,succen){
		this.loader=0;
		this.succen=succen;
		this.load=load;
		var _this=this;
		if(url.length){
			this.AudioStore=[];
			for(let i=0;i<url.length;i++){
				let dom=document.createElement("audio");
				dom.volume=0.8;
				dom.src=url[i];
				dom.controls=false;
				this.AudioStore.push(dom);
				dom.oncanplaythrough=function(){
					_this.loader+=100/url.length;
					_this.getSuccen();
				}
			}
		}else{
			this.AudioStore={};
			let i;
			for(i in url){
				let doms=document.createElement("audio");
				doms.volume=0.8;
				doms.src=url[i];
				doms.controls=false;
				this.AudioStore[i]=doms;
				doms.oncanplaythrough=function(){
					_this.loader+=100/Object.values(url).length;
					_this.getSuccen();
				}
			}
		}
	}
	GameAudio.prototype.getLoad=function(){
		return this.loader/100;
	}
	GameAudio.prototype.Play=function(x){
		if(x.length==1){
			this.AudioStore[x[0]].play();
		}else{
			var _this=this;
			this.AudioStore[x[0]].play();
			function is(){
				x.shift();
				if(x.length==0){
					return false;
				}
				_this.AudioStore[x[0]].play();
				_this.AudioStore[x[0]].onended=is;
			}
			this.AudioStore[x[0]].onended=is;
		}
	}
	GameAudio.prototype.getSuccen=function(){
		if(this.loader>=100){
			if(typeof(this.succen)=="function"){
				this.succen();
			}
			this.getSuccen=function(){};
		}else{
			if(typeof(this.load)=="function"){
				this.load();
			}
		}
	}
	GameAudio.prototype.setVolume=function(x){
		if(this.AudioStore.length){
			for(let i=0;i<this.AudioStore.length;i++){
				this.AudioStore[i].volume=x/100;
			}
		}else{
			let i;
			for(i in this.AudioStore){
				this.AudioStore[i].volume=x/100;
			}
		}
	}
	return GameAudio;
}())

window.liveInit=function(str){
	if(str==null){
		return false;
	}
	let a=str;
	let index=parseInt(a[1]);
	let data=false;
	let msg={index,data}
	if(a.indexOf('a')!=-1){
		app.$store.dispatch("setVideoStatus",msg);
		clearInterval(videoSet[msg.index]);
	}else{
		msg.index+=2;
		app.$store.dispatch("setVideoStatus",msg);
		clearInterval(videoSet[msg.index]);
	}
}
window.videoSet=[1,1,1,1,1,1];




Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
